{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import nltk, string\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "import re\n",
    "import pandas as pd\n",
    "from nltk.tokenize import WordPunctTokenizer\n",
    "from keras.models import model_from_json\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# read dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>business_id</th>\n",
       "      <th>review_id</th>\n",
       "      <th>stars</th>\n",
       "      <th>text</th>\n",
       "      <th>user_id</th>\n",
       "      <th>cool</th>\n",
       "      <th>useful</th>\n",
       "      <th>funny</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9yKzy9PApeiPPOUJEtnvkg</td>\n",
       "      <td>fWKvX83p0-ka4JS3dc6E5A</td>\n",
       "      <td>5</td>\n",
       "      <td>my wife took me here on my birthday for breakf...</td>\n",
       "      <td>rLtl8ZkDX5vH5nAx9C3q5Q</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              business_id               review_id  stars  \\\n",
       "0  9yKzy9PApeiPPOUJEtnvkg  fWKvX83p0-ka4JS3dc6E5A      5   \n",
       "\n",
       "                                                text                 user_id  \\\n",
       "0  my wife took me here on my birthday for breakf...  rLtl8ZkDX5vH5nAx9C3q5Q   \n",
       "\n",
       "   cool  useful  funny  \n",
       "0     2       5      0  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('dataset_food_online.txt' ,encoding=\"ISO-8859-1\")\n",
    "tok = WordPunctTokenizer()\n",
    "def cleaner(text):\n",
    "    letters_only = re.sub(\"[^a-zA-Z]\", \" \", text)\n",
    "    lower_case = letters_only.lower()\n",
    "    words = tok.tokenize(lower_case)\n",
    "    return (\" \".join(words)).strip()\n",
    "data['text']=data['text'].apply(lambda x: cleaner(x))\n",
    "data = data.drop('date', 1)\n",
    "data = data.drop('type', 1)\n",
    "#data = data.drop('review_id', 1)\n",
    "#data = data.drop('business_id', 1)\n",
    "data.head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# load LSTM model and embedding matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LSTM loaded\n",
      "(28626, 200)\n"
     ]
    }
   ],
   "source": [
    "# Model reconstruction from JSON file\n",
    "with open('model_architecture.json', 'r') as f:\n",
    "    model = model_from_json(f.read())\n",
    "\n",
    "# Load weights into the new model\n",
    "model.load_weights('model_weights.h5')\n",
    "print(\"LSTM loaded\")\n",
    "\n",
    "embeddings = numpy.load(\"embeddings.dat\")\n",
    "print(embeddings.shape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# user input reveiw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input a text for recommender:falafel\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>business_id</th>\n",
       "      <th>review_id</th>\n",
       "      <th>stars</th>\n",
       "      <th>text</th>\n",
       "      <th>user_id</th>\n",
       "      <th>cool</th>\n",
       "      <th>useful</th>\n",
       "      <th>funny</th>\n",
       "      <th>sim</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3155</th>\n",
       "      <td>doEZAj-NSnoEMOJl5yXcNw</td>\n",
       "      <td>C4lduID0NZMqzjQuIWlnbw</td>\n",
       "      <td>5</td>\n",
       "      <td>falafel is outstanding here dark exterior with...</td>\n",
       "      <td>joIzw_aUiNvBTuGoytrH7g</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.207513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9445</th>\n",
       "      <td>n_YHTTG0QIjZ3055wReefQ</td>\n",
       "      <td>mj1KE6poy83L-rxaWMGS7w</td>\n",
       "      <td>4</td>\n",
       "      <td>we stumbled upon this little place in a worn s...</td>\n",
       "      <td>N7lSh49zQ13sG3Jpa9E6cw</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0.202823</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2455</th>\n",
       "      <td>6oRAC4uyJCsJl1X0WZpVSA</td>\n",
       "      <td>XABPPSphqJ4PVrZbZ_AjtA</td>\n",
       "      <td>4</td>\n",
       "      <td>haji baba is unique by being both a restaurant...</td>\n",
       "      <td>kq5Pdsy8Znyh9KEkxWT_QA</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.199213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1684</th>\n",
       "      <td>Aicnm12Zped8nQFXyRRFvw</td>\n",
       "      <td>OHkVZtMNwwUoSThKHBzb3A</td>\n",
       "      <td>4</td>\n",
       "      <td>byblos has some of the best middle eastern foo...</td>\n",
       "      <td>nWouNfZD3Pw08RYizxkqcA</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.146753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5084</th>\n",
       "      <td>-ftQeUsqwDkExRg6IYrubQ</td>\n",
       "      <td>fEVIIMjwaFJmjuC4BboVzg</td>\n",
       "      <td>5</td>\n",
       "      <td>i am from chicago and the italian beef here re...</td>\n",
       "      <td>MGYI7Ip_hs6TIt4goS_mbA</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.143728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 business_id               review_id  stars  \\\n",
       "3155  doEZAj-NSnoEMOJl5yXcNw  C4lduID0NZMqzjQuIWlnbw      5   \n",
       "9445  n_YHTTG0QIjZ3055wReefQ  mj1KE6poy83L-rxaWMGS7w      4   \n",
       "2455  6oRAC4uyJCsJl1X0WZpVSA  XABPPSphqJ4PVrZbZ_AjtA      4   \n",
       "1684  Aicnm12Zped8nQFXyRRFvw  OHkVZtMNwwUoSThKHBzb3A      4   \n",
       "5084  -ftQeUsqwDkExRg6IYrubQ  fEVIIMjwaFJmjuC4BboVzg      5   \n",
       "\n",
       "                                                   text  \\\n",
       "3155  falafel is outstanding here dark exterior with...   \n",
       "9445  we stumbled upon this little place in a worn s...   \n",
       "2455  haji baba is unique by being both a restaurant...   \n",
       "1684  byblos has some of the best middle eastern foo...   \n",
       "5084  i am from chicago and the italian beef here re...   \n",
       "\n",
       "                     user_id  cool  useful  funny       sim  \n",
       "3155  joIzw_aUiNvBTuGoytrH7g     1       1      1  0.207513  \n",
       "9445  N7lSh49zQ13sG3Jpa9E6cw     3       3      2  0.202823  \n",
       "2455  kq5Pdsy8Znyh9KEkxWT_QA     0       0      0  0.199213  \n",
       "1684  nWouNfZD3Pw08RYizxkqcA     1       0      1  0.146753  \n",
       "5084  MGYI7Ip_hs6TIt4goS_mbA     0       0      0  0.143728  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev=input(\"input a text for recommender:\")\n",
    "#-----------------------------------------\n",
    "#nltk.download('punkt') # if necessary...\n",
    "stemmer = nltk.stem.porter.PorterStemmer()\n",
    "remove_punctuation_map = dict((ord(char), None) for char in string.punctuation)\n",
    "\n",
    "def stem_tokens(tokens):\n",
    "    return [stemmer.stem(item) for item in tokens]\n",
    "def normalize(text):\n",
    "    return stem_tokens(nltk.word_tokenize(text.lower().translate(remove_punctuation_map)))\n",
    "vectorizer = TfidfVectorizer(tokenizer=normalize, stop_words='english')\n",
    "def cosine_sim(text1, text2):\n",
    "    tfidf = vectorizer.fit_transform([text1, text2])\n",
    "    return ((tfidf * tfidf.T).A)[0,1]\n",
    "rank_new1=[]\n",
    "for index, row in data.iterrows():\n",
    "    rank_new1.append(cosine_sim(row['text'], rev))\n",
    "\n",
    "data['sim']=rank_new1\n",
    "final_df = data.sort_values(by=['sim'], ascending=False)\n",
    "final_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/raminm/anaconda3/envs/tensorflow/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 28625 unique tokens.\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from sklearn import preprocessing\n",
    "from sklearn.cross_validation import train_test_split as sk_split\n",
    "from keras.layers.embeddings import Embedding\n",
    "from keras.layers.recurrent import LSTM\n",
    "from keras.layers import Dropout\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "from keras.preprocessing.text import Tokenizer\n",
    "from keras.preprocessing.sequence import pad_sequences\n",
    "import re\n",
    "max_fatures = 2000\n",
    "tokenizer = Tokenizer(num_words=max_fatures, split=' ')\n",
    "tokenizer.fit_on_texts(final_df['text'])\n",
    "sequences=tokenizer.fit_on_texts(final_df['text'].values)\n",
    "X = tokenizer.texts_to_sequences(final_df['text'].values)\n",
    "word_index = tokenizer.word_index\n",
    "print('Found %s unique tokens.' % len(word_index))\n",
    "X = pad_sequences(X,maxlen=200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred=model.predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>business_id</th>\n",
       "      <th>review_id</th>\n",
       "      <th>stars</th>\n",
       "      <th>text</th>\n",
       "      <th>user_id</th>\n",
       "      <th>cool</th>\n",
       "      <th>useful</th>\n",
       "      <th>funny</th>\n",
       "      <th>sim</th>\n",
       "      <th>newranke</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3155</th>\n",
       "      <td>doEZAj-NSnoEMOJl5yXcNw</td>\n",
       "      <td>C4lduID0NZMqzjQuIWlnbw</td>\n",
       "      <td>5</td>\n",
       "      <td>falafel is outstanding here dark exterior with...</td>\n",
       "      <td>joIzw_aUiNvBTuGoytrH7g</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.207513</td>\n",
       "      <td>4.331992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9445</th>\n",
       "      <td>n_YHTTG0QIjZ3055wReefQ</td>\n",
       "      <td>mj1KE6poy83L-rxaWMGS7w</td>\n",
       "      <td>4</td>\n",
       "      <td>we stumbled upon this little place in a worn s...</td>\n",
       "      <td>N7lSh49zQ13sG3Jpa9E6cw</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0.202823</td>\n",
       "      <td>4.030197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2455</th>\n",
       "      <td>6oRAC4uyJCsJl1X0WZpVSA</td>\n",
       "      <td>XABPPSphqJ4PVrZbZ_AjtA</td>\n",
       "      <td>4</td>\n",
       "      <td>haji baba is unique by being both a restaurant...</td>\n",
       "      <td>kq5Pdsy8Znyh9KEkxWT_QA</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.199213</td>\n",
       "      <td>4.090116</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 business_id               review_id  stars  \\\n",
       "3155  doEZAj-NSnoEMOJl5yXcNw  C4lduID0NZMqzjQuIWlnbw      5   \n",
       "9445  n_YHTTG0QIjZ3055wReefQ  mj1KE6poy83L-rxaWMGS7w      4   \n",
       "2455  6oRAC4uyJCsJl1X0WZpVSA  XABPPSphqJ4PVrZbZ_AjtA      4   \n",
       "\n",
       "                                                   text  \\\n",
       "3155  falafel is outstanding here dark exterior with...   \n",
       "9445  we stumbled upon this little place in a worn s...   \n",
       "2455  haji baba is unique by being both a restaurant...   \n",
       "\n",
       "                     user_id  cool  useful  funny       sim  newranke  \n",
       "3155  joIzw_aUiNvBTuGoytrH7g     1       1      1  0.207513  4.331992  \n",
       "9445  N7lSh49zQ13sG3Jpa9E6cw     3       3      2  0.202823  4.030197  \n",
       "2455  kq5Pdsy8Znyh9KEkxWT_QA     0       0      0  0.199213  4.090116  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_df['newranke']=pred\n",
    "final_df.head(3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>business_id</th>\n",
       "      <th>review_id</th>\n",
       "      <th>stars</th>\n",
       "      <th>text</th>\n",
       "      <th>user_id</th>\n",
       "      <th>cool</th>\n",
       "      <th>useful</th>\n",
       "      <th>funny</th>\n",
       "      <th>sim</th>\n",
       "      <th>newranke</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3155</th>\n",
       "      <td>doEZAj-NSnoEMOJl5yXcNw</td>\n",
       "      <td>C4lduID0NZMqzjQuIWlnbw</td>\n",
       "      <td>5</td>\n",
       "      <td>falafel is outstanding here dark exterior with...</td>\n",
       "      <td>joIzw_aUiNvBTuGoytrH7g</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.207513</td>\n",
       "      <td>4.331992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9445</th>\n",
       "      <td>n_YHTTG0QIjZ3055wReefQ</td>\n",
       "      <td>mj1KE6poy83L-rxaWMGS7w</td>\n",
       "      <td>4</td>\n",
       "      <td>we stumbled upon this little place in a worn s...</td>\n",
       "      <td>N7lSh49zQ13sG3Jpa9E6cw</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0.202823</td>\n",
       "      <td>4.030197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2455</th>\n",
       "      <td>6oRAC4uyJCsJl1X0WZpVSA</td>\n",
       "      <td>XABPPSphqJ4PVrZbZ_AjtA</td>\n",
       "      <td>4</td>\n",
       "      <td>haji baba is unique by being both a restaurant...</td>\n",
       "      <td>kq5Pdsy8Znyh9KEkxWT_QA</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.199213</td>\n",
       "      <td>4.090116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1684</th>\n",
       "      <td>Aicnm12Zped8nQFXyRRFvw</td>\n",
       "      <td>OHkVZtMNwwUoSThKHBzb3A</td>\n",
       "      <td>4</td>\n",
       "      <td>byblos has some of the best middle eastern foo...</td>\n",
       "      <td>nWouNfZD3Pw08RYizxkqcA</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.146753</td>\n",
       "      <td>4.602813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5084</th>\n",
       "      <td>-ftQeUsqwDkExRg6IYrubQ</td>\n",
       "      <td>fEVIIMjwaFJmjuC4BboVzg</td>\n",
       "      <td>5</td>\n",
       "      <td>i am from chicago and the italian beef here re...</td>\n",
       "      <td>MGYI7Ip_hs6TIt4goS_mbA</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.143728</td>\n",
       "      <td>4.525226</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 business_id               review_id  stars  \\\n",
       "3155  doEZAj-NSnoEMOJl5yXcNw  C4lduID0NZMqzjQuIWlnbw      5   \n",
       "9445  n_YHTTG0QIjZ3055wReefQ  mj1KE6poy83L-rxaWMGS7w      4   \n",
       "2455  6oRAC4uyJCsJl1X0WZpVSA  XABPPSphqJ4PVrZbZ_AjtA      4   \n",
       "1684  Aicnm12Zped8nQFXyRRFvw  OHkVZtMNwwUoSThKHBzb3A      4   \n",
       "5084  -ftQeUsqwDkExRg6IYrubQ  fEVIIMjwaFJmjuC4BboVzg      5   \n",
       "\n",
       "                                                   text  \\\n",
       "3155  falafel is outstanding here dark exterior with...   \n",
       "9445  we stumbled upon this little place in a worn s...   \n",
       "2455  haji baba is unique by being both a restaurant...   \n",
       "1684  byblos has some of the best middle eastern foo...   \n",
       "5084  i am from chicago and the italian beef here re...   \n",
       "\n",
       "                     user_id  cool  useful  funny       sim  newranke  \n",
       "3155  joIzw_aUiNvBTuGoytrH7g     1       1      1  0.207513  4.331992  \n",
       "9445  N7lSh49zQ13sG3Jpa9E6cw     3       3      2  0.202823  4.030197  \n",
       "2455  kq5Pdsy8Znyh9KEkxWT_QA     0       0      0  0.199213  4.090116  \n",
       "1684  nWouNfZD3Pw08RYizxkqcA     1       0      1  0.146753  4.602813  \n",
       "5084  MGYI7Ip_hs6TIt4goS_mbA     0       0      0  0.143728  4.525226  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=final_df[:20]\n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>business_id</th>\n",
       "      <th>review_id</th>\n",
       "      <th>stars</th>\n",
       "      <th>text</th>\n",
       "      <th>user_id</th>\n",
       "      <th>cool</th>\n",
       "      <th>useful</th>\n",
       "      <th>funny</th>\n",
       "      <th>sim</th>\n",
       "      <th>newranke</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1684</th>\n",
       "      <td>Aicnm12Zped8nQFXyRRFvw</td>\n",
       "      <td>OHkVZtMNwwUoSThKHBzb3A</td>\n",
       "      <td>4</td>\n",
       "      <td>byblos has some of the best middle eastern foo...</td>\n",
       "      <td>nWouNfZD3Pw08RYizxkqcA</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.146753</td>\n",
       "      <td>4.602813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4134</th>\n",
       "      <td>doEZAj-NSnoEMOJl5yXcNw</td>\n",
       "      <td>BX_iL7m8Y6_J3nNFCbp_dw</td>\n",
       "      <td>4</td>\n",
       "      <td>stopped in last night for a take out dinner i ...</td>\n",
       "      <td>TTP7l5g4QUVCpQZ9NTAWqw</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0.124318</td>\n",
       "      <td>4.587102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5084</th>\n",
       "      <td>-ftQeUsqwDkExRg6IYrubQ</td>\n",
       "      <td>fEVIIMjwaFJmjuC4BboVzg</td>\n",
       "      <td>5</td>\n",
       "      <td>i am from chicago and the italian beef here re...</td>\n",
       "      <td>MGYI7Ip_hs6TIt4goS_mbA</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.143728</td>\n",
       "      <td>4.525226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2453</th>\n",
       "      <td>fb9eLHJ4S--TyXsarJJo-g</td>\n",
       "      <td>wiOFzFx_gL9yF7Qn_TAG8Q</td>\n",
       "      <td>5</td>\n",
       "      <td>this place is still open but the name has been...</td>\n",
       "      <td>qYOtHoVCMkdmh73wvUohIA</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.072055</td>\n",
       "      <td>4.488928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3255</th>\n",
       "      <td>w19cemjVR8u02PgjFpJ7Mw</td>\n",
       "      <td>gS_cpr4aTV5nE-3iyKtnVQ</td>\n",
       "      <td>5</td>\n",
       "      <td>so i have to admit that i once threw up in the...</td>\n",
       "      <td>fhV21-QB6n402-J9vR-7cQ</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0.066784</td>\n",
       "      <td>4.457037</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 business_id               review_id  stars  \\\n",
       "1684  Aicnm12Zped8nQFXyRRFvw  OHkVZtMNwwUoSThKHBzb3A      4   \n",
       "4134  doEZAj-NSnoEMOJl5yXcNw  BX_iL7m8Y6_J3nNFCbp_dw      4   \n",
       "5084  -ftQeUsqwDkExRg6IYrubQ  fEVIIMjwaFJmjuC4BboVzg      5   \n",
       "2453  fb9eLHJ4S--TyXsarJJo-g  wiOFzFx_gL9yF7Qn_TAG8Q      5   \n",
       "3255  w19cemjVR8u02PgjFpJ7Mw  gS_cpr4aTV5nE-3iyKtnVQ      5   \n",
       "\n",
       "                                                   text  \\\n",
       "1684  byblos has some of the best middle eastern foo...   \n",
       "4134  stopped in last night for a take out dinner i ...   \n",
       "5084  i am from chicago and the italian beef here re...   \n",
       "2453  this place is still open but the name has been...   \n",
       "3255  so i have to admit that i once threw up in the...   \n",
       "\n",
       "                     user_id  cool  useful  funny       sim  newranke  \n",
       "1684  nWouNfZD3Pw08RYizxkqcA     1       0      1  0.146753  4.602813  \n",
       "4134  TTP7l5g4QUVCpQZ9NTAWqw     1       5      0  0.124318  4.587102  \n",
       "5084  MGYI7Ip_hs6TIt4goS_mbA     0       0      0  0.143728  4.525226  \n",
       "2453  qYOtHoVCMkdmh73wvUohIA     0       1      0  0.072055  4.488928  \n",
       "3255  fhV21-QB6n402-J9vR-7cQ     2       3      1  0.066784  4.457037  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.sort_values(by=['newranke'], ascending=False)\n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1684    Aicnm12Zped8nQFXyRRFvw\n",
       "4134    doEZAj-NSnoEMOJl5yXcNw\n",
       "5084    -ftQeUsqwDkExRg6IYrubQ\n",
       "2453    fb9eLHJ4S--TyXsarJJo-g\n",
       "3255    w19cemjVR8u02PgjFpJ7Mw\n",
       "2244    Gw8DsQQCO5m1hggXqwynFg\n",
       "3591    tyETqrYijm3cY4noCwl9Ww\n",
       "6006    nRO4tRwimU12hg7Cnz__iA\n",
       "3155    doEZAj-NSnoEMOJl5yXcNw\n",
       "908     yktWUtKBja_Lzk3wwR6RFA\n",
       "2455    6oRAC4uyJCsJl1X0WZpVSA\n",
       "9445    n_YHTTG0QIjZ3055wReefQ\n",
       "3014    6oRAC4uyJCsJl1X0WZpVSA\n",
       "2135    qXQ3ZBdwI3GlbR5-eYWqNA\n",
       "6653    8m08a9xJKmANwmeuR-0bPA\n",
       "1123    w19cemjVR8u02PgjFpJ7Mw\n",
       "2736    zonV2F6YNVn2_sI5dK82eg\n",
       "5511    dn9sB0Kok8cnkDhpcl7YCg\n",
       "2868    cOUS79i4vltKIc_hy4OZBg\n",
       "9272    VVPVg9aJzNczTgeM36TGJw\n",
       "Name: business_id, dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df[:20]['business_id']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
